package com.mjx.algorithm.oj;

import java.util.*;

/**
 * // TODO: 2017/2/12 0012
 * 转金额中文
 * Created by 梅京翔 on 2017 02 11下午 8:49.
 */
public class problem1235 {
    public static void main(String[] args) {
        Map<Integer, String> numMap = new HashMap<Integer, String>();
        Map<Integer, String> map = new HashMap<Integer, String>();
        numMap.put(0, "零");
        numMap.put(1, "壹");
        numMap.put(2, "贰");
        numMap.put(3, "叁");
        numMap.put(4, "肆");
        numMap.put(5, "伍");
        numMap.put(6, "陆");
        numMap.put(7, "柒");
        numMap.put(8, "捌");
        numMap.put(9, "玖");
        numMap.put(10, "拾");
        map.put(1, "");
        map.put(2, "拾");
        map.put(3, "佰");
        map.put(4, "千");
        map.put(5, "万");
        map.put(6, "拾");
        map.put(7, "佰");
        map.put(8, "千");
        map.put(9, "亿");
        Stack<String> stringStack = new Stack<String>();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String numberStr = scanner.nextLine();
            ArrayList<String> numberArray = new ArrayList<String>();
            processBigInteger(numberStr, numberArray);
            getDecimal(numberStr, stringStack, numMap);
            getInteger(numberArray, numMap, map, stringStack);
            while (!stringStack.empty()) {
                System.out.print(stringStack.pop());
            }
            System.out.println();
        }
    }

    /**
     * 将字符串拆分为几个字符串
     * @param numberStr
     * @param numberArray
     */
    private static void processBigInteger(String numberStr, ArrayList numberArray) {
        String[] s = numberStr.split("\\.");
        String integerS = s[0];
        if (integerS.length() > 9) {
            numberArray.add(integerS.substring(integerS.length() - 9, integerS.length()));
            processBigInteger(integerS.substring(0, integerS.length() - 9), numberArray);
        } else
            numberArray.add(integerS);
    }

    /**
     * 获取小数部分
     * @param numberStr
     * @param stringStack
     * @param numMap
     */
    private static void getDecimal(String numberStr,Stack<String> stringStack,Map numMap){
        double decimal = 0;
        if (numberStr.split("\\.").length > 1)
            decimal= Double.parseDouble(numberStr.split("\\.")[1]);
        if (decimal > 0) {
            if (decimal < 10)
                decimal*=10;
            int fenNum = (int) decimal % 10;
            if (fenNum > 0)
                stringStack.push(numMap.get(fenNum) + "分");
            int jiaoNum = (int) decimal / 10;
            if (jiaoNum > 0)
                stringStack.push(numMap.get(jiaoNum)+ "角");
        }
        else
            stringStack.push("整");
    }

    private static void getInteger(ArrayList<String> numberArray, Map numMap, Map map, Stack<String> stringStack){
        String numStr = numberArray.get(0);
        stringStack.push("元");
        int step = 1;
        int curPos = 0;
        int length = numStr.length();
        int count0 = 0;//连续0次数
        int start0Pos = step;
        for (int i = length - 1; i >= 0; i--) {
            int n = Character.getNumericValue(numStr.charAt(i));
            if (n == 0) {
                if (count0 == 0)
                    start0Pos = step;//记录第一次零的位置
                if (step == 5 || step == 9) {//当到万，亿时，开始处理其之前的连续零
                    if (start0Pos < step){
                        if (!(start0Pos == 5 || start0Pos == 1))//如果连续0的第一位是元万亿时，将零都去掉，否则留下一个零
                            count0--;
                        while (count0-- > 0)
                            stringStack.pop();
                        if (step >=9 && start0Pos == 5)//当千万，百万，十万，万都没空时，多出栈一次，将万去掉
                            stringStack.pop();
                        count0 = 0;
                        start0Pos = step;
                    }
                }
                count0++;
            }
            else {
                if (count0 > 1){
                    if (!(start0Pos == 5 || start0Pos == 1))//如果连续0的第一位是元万亿时，将零都去掉，否则留下一个零
                        count0--;
                    while (count0-- > 0)
                        stringStack.pop();
                    if (step >=9 && start0Pos == 5)//当千万，百万，十万，万都没空时，多出栈一次，将万去掉
                        stringStack.pop();
                    count0 = 0;
                }
                if (count0 == 1 && (start0Pos == 1 || start0Pos == 5 || start0Pos == 9)) {//例子：10，100000
                    stringStack.pop();
                    count0 = 0;
                }
            }
            StringBuffer s = new StringBuffer();
            s.append(numMap.get(n));
            if (n != 0)//数字不等于0时，百千万不省略
                s.append(map.get(step));
            if (n == 0 && (step == 1 || step == 5 || step == 9)) {//当是万或亿 遇到零则不省略零，反例 贰拾零亿
                s.append(map.get(step));
            }
            if (step == 1 || step == 5 || step ==9){//元，万，亿和数字分开入栈
                stringStack.push(map.get(step).toString());
                s.delete(0,s.length());
                s.append(numMap.get(n));
            }
            if (step == 2 || step == 6){//如果是拾位
                if (n == 1){
                    if (i == 0 && curPos + 1 == numberArray.size()){
                        s.delete(0,s.length());
                        s.append(map.get(step));
                    }
                }
            }
            stringStack.push(s.toString());
            step++;
            if (step > 9)//超过亿时，再次从拾开始
                step -= 8;
            if (i-1 < 0 && curPos + 1 < numberArray.size()) {//数字大于9亿时分割了字符串，开始处理高位
                numStr = numberArray.get(++curPos);
                length = numStr.length();
                i = length;//之后for还会执行一次i--,所以i=length
            }
        }
    }
}
